package com.airbat.prize.utils;

import com.alibaba.fastjson.JSONObject;
import jodd.util.StringUtil;
import oracle.sql.CLOB;

import java.io.BufferedReader;
import java.io.Reader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author Wumj
 * @Date 2021/8/31 16:52
 * @describe 驼峰转换工具
 * @Version 1.0
 */

public class StringCaseUtil {


    public StringCaseUtil() throws Exception {
    }

    public static String change(String sourceStr, String linkStr) {
        String s1 = sourceStr.toLowerCase();
        char[] charArr = s1.toCharArray();
        charArr[0] = (char) (charArr[0] & 0xdf);
        for (int i = 1; i < charArr.length; i++) {
            if (String.valueOf(charArr[i - 1]).equals(linkStr)) {
                charArr[i] = (char) (charArr[i] & 0xdf);
            }
        }
        String temp = String.copyValueOf(charArr);
        return temp.replace(linkStr, "");
    }

    /**
     * 清洗条件转换成正则表达式
     *
     * @param contidion
     * @return
     */
    public static Pattern StringToPattern(String contidion) {
        String[] keywords = contidion.split("[,，]");
        String regStr1 = "(";
        for (String word : keywords) {
            if (StringUtil.isNotEmpty(word)) {
                if (regStr1.endsWith("(")) {
                    regStr1 += word;
                } else {
                    regStr1 += "|" + word;
                }
            }
        }
        regStr1 += ")";
        Pattern pattern = Pattern.compile(regStr1);
        return pattern;
    }

    /**
     * 清洗条件转换成正则表达式
     *
     * @param keywords
     * @return
     */
    public static Pattern listStringToPattern(List<String> keywords) {
        String regStr1 = "(";
        for (String word : keywords) {
            if (StringUtil.isNotEmpty(word)) {
                if (regStr1.endsWith("(")) {
                    regStr1 += word;
                } else {
                    regStr1 += "|" + word;
                }
            }
        }
        regStr1 += ")";
        Pattern pattern = Pattern.compile(regStr1);
        return pattern;
    }

    public static String ClobToString(CLOB clob) {
        try {
            String reString = "";
            Reader is = clob.getCharacterStream();// 得到流
            BufferedReader br = new BufferedReader(is);
            String s = br.readLine();
            StringBuffer sb = new StringBuffer();
            while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
                sb.append(s + "\n");
                s = br.readLine();
            }
            reString = sb.toString();
            return reString;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    public static String camelToLine(String sourceStr) {
        if (sourceStr == null)
            return null;
        return sourceStr.replaceAll("[A-Z]", "_$0").toLowerCase();
    }
    public static String lineToCamel(String sourceStr, String linkStr){
            String s1=sourceStr.toLowerCase();
            char[] charArr=s1.toCharArray();
            charArr[0]= (char) (charArr[0]&0xdf);
            for(int i=1;i<charArr.length;i++){
                if(String.valueOf(charArr[i-1]).equals(linkStr)){
                    if(i>2) {
                        charArr[i] = (char) (charArr[i] & 0xdf);
                    }else {//防止类似 e_id 生成 setEId方法
                        continue;
                    }
                }
            }
            String temp=String.copyValueOf(charArr);
            return temp.replace(linkStr,"");
    }
    public static <T> List<T> decode(Class<T> clazz, List<JSONObject> dataList){
        List<T> result = new ArrayList<>();
        try {
            if (dataList.size() > 0) {
                for (int j = 0; j < dataList.size(); j++) {
                    T t = clazz.newInstance();
                    JSONObject jsonObject=dataList.get(j);
                    for (String key:jsonObject.keySet()) {
                        Object obj = "";
                        String methodName=lineToCamel(key,"_");
                        try {
                            Class c = String.class;
                            String data = jsonObject.getString(key);
                            obj = data;
                            String columnname = "set" + methodName;
                            Method setMethod = t.getClass().getMethod(columnname, new Class[]{c});
                            setMethod.invoke(t, obj);
                        } catch (NoSuchMethodException e) {
//                            log.info(obj + "", e);
                        }
                    }
                    result.add(t);
                }
            }
        }catch (Exception e){
//            log.info("解析出错"+rexmlinfo,e);
        }
        return result;
    }

    public static String arrToIn(List<String> strArr) {
        StringBuilder idArr = new StringBuilder();
        for (int i = 0; i < strArr.size(); i++) {
            if (i == 0) {
                idArr.append("'").append(strArr.get(i)).append("'");
            } else {
                idArr.append(",'").append(strArr.get(i)).append("'");
            }
        }
        return idArr.toString();
    }


}
